package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;


@Service
@Transactional//开启事务
public class CategoryServiceImpl implements CategoryService {


    @Autowired(required = false)
    CategoryMapper categoryMapper;

    @Autowired
    DishMapper dishMapper;

    @Autowired
    SetmealMapper setmealMapper;

    /**
     * 添加分类
     *
     * @param dto
     */
    @Override
    public Result addCategory(CategoryDTO dto) {
        //处理参数
        if (dto.getName() == null){
            return Result.error(MessageConstant.CATEGORY_NOT_EXISTS_FOUND);
        }

        //整理添加参数到实体类
        Category category = new Category();
        //1.复制属性
        BeanUtils.copyProperties(dto , category);
        category.setStatus(StatusConstant.DISABLE);

        /*//2.添加补全属性 以后在切面类完成
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        category.setCreateUser(BaseContext.getCurrentId());
        category.setUpdateUser(BaseContext.getCurrentId());*/

        //判断分类是否存在,存在返回相关信息,调用dto
        Category ctg = categoryMapper.getByname(category.getName());
        //判断
        if(ctg != null){
            return Result.error(MessageConstant.CATEGORY_EXISTS_FOUND);
        }

        //调用dao
        categoryMapper.addCategory(category);

        //返回参数
        return Result.success();
    }

    /**
     * 分页条件查询
     *
     * @param dto
     * @return
     */
    @Override
    public Result page(CategoryPageQueryDTO dto) {
        //处理参数,给默认参数
        if (dto.getPage() == null){
            dto.setPage(1);
        }
        if (dto.getPageSize() == null){
            dto.setPageSize(10);
        }

        //开启分页
        PageHelper.startPage(dto.getPage(),dto.getPageSize());

        //调用dao
        Page<Category> categoryPage = categoryMapper.page(dto);

        //封装到PageResult返回
        PageResult pageResult = new PageResult();
        pageResult.setTotal(categoryPage.getTotal());
        pageResult.setRecords(categoryPage.getResult());

        //返回数据
        return Result.success(pageResult);
    }

    /**
     * 根据id删除分类
     *
     * @param id
     * @return
     */
    @Override
    @Transactional
    public Result deleteById(Integer id) {
        //判空
        if (id == null){
            return Result.error(MessageConstant.ID_EXISTS_FOUND);
        }
        //查询判断菜品分类下面是否有关联菜品,有就抛异常
        Integer dishCount = dishMapper.selectCountByCategoryId(id);
        if (dishCount > 0){
            return Result.error(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }

        //询查判断套餐分类下面是否有关联套餐,有就抛异常
        Integer setmealCount = setmealMapper.selectCountByCategoryId(id);
        if (setmealCount > 0 ){
            return Result.error(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }

        //调用dao
        categoryMapper.deleteById(id);

        return Result.success();
    }

    /**
     * 修改状态
     *
     * @param status
     * @param id
     */
    @Override
    public void updateStatus(Integer status, Integer id) {
        //处理参数
        if (id == null){
            throw new RuntimeException(MessageConstant.ID_EXISTS_FOUND);
        }

        //封装参数到实体类进行修改
        Category category = new Category();
        category.setStatus(status);
        category.setId(id.longValue());
        /*以后在切面类完成添加操作
        category.setUpdateUser(BaseContext.getCurrentId());
        category.setUpdateTime(LocalDateTime.now());
        */


        //调用dao
        categoryMapper.update(category);

    }

    /**
     * 修改分类
     *
     * @param dto
     */
    @Override
    public void update(CategoryDTO dto) {
        //参数处理
        if (dto.getId() == null){
            throw new RuntimeException(MessageConstant.ID_EXISTS_FOUND);
        }

        //封装数据到实体类
        Category category = new Category();
        //拷贝
        BeanUtils.copyProperties(dto,category);
        /*//补全参数  以后在切面类完成添加操作
        category.setUpdateTime(LocalDateTime.now());
        category.setUpdateUser(BaseContext.getCurrentId());*/

        categoryMapper.update(category);
    }

    /**
     * 根据类型查询分类
     *
     * @param type
     * @return
     */
    @Override
    public Result list(Integer type) {
        //根据类型查询分类数据
        List<Category> categoryList = categoryMapper.getByType(type);

        return Result.success(categoryList);
    }


}
